package cn.imvc.app.models;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;

import cn.imvc.app.entities.Area;
import cn.imvc.app.repositories.AreaRepository;
import cn.imvc.data.domain.ViewRequest;

@Service
public class AreaModelImpl implements AreaModel {

	@Autowired
	private AreaRepository areaRepository;

	public Area get(int id) {
		return areaRepository.findOne(id);
	}

	public Page<Area> find(ViewRequest viewRequest) {
		return (Page<Area>) areaRepository.findAll();
	}

	public Area save(Area area) {
		Area area2 = areaRepository.saveAndFlush(area);
		return area2;
	}

	public Area update(Area newArea) {
		Area loadArea = get(newArea.getId());
		BeanUtils.copyProperties(newArea, loadArea, new String[] { "id" });
		return areaRepository.saveAndFlush(loadArea);
	}

	public void delete(int id) {
		areaRepository.delete(id);
	}

	public void deleteBatch(int[] ids) {
		for (int id : ids) {
			delete(id);
		}
	}
	
	public List<Area> findByName(String name){
		return areaRepository.findByName(name);
	}
	
	public List<Area> findByName(String name, Area parent){
		return areaRepository.findByNameAndParent(name, parent);
	}
	
	public String areaToJson() {
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		List<Area> list = areaRepository.findByParentIsNullOrderByIdAsc();
		for (int i = 0; i < list.size(); i++) {
			Area area = list.get(i);
			if(area.getParent() == null){
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("name", area.getName());
				map.put("code", String.valueOf(area.getId()));
				List<Area> list1 = areaRepository.findByParentOrderByIdAsc(area);
				if(list1.size() > 0){
					map.put("sub", getSub(area));
				}
				map.put("type", "0");
				result.add(map);
			}
		}
		return JSON.toJSONString(result);
	}
	
	private List<Map<String, Object>> getSub(Area area){
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		List<Area> list = areaRepository.findByParentOrderByIdAsc(area);
		if(list.size() > 0){
			for (int i = 0; i < list.size(); i++) {
				Area area1 = list.get(i);
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("name", area1.getName());
				map.put("code", String.valueOf(area1.getId()));
				List<Area> list2 = areaRepository.findByParentOrderByIdAsc(area1);
				if(list2.size() > 0){
					map.put("sub", getSub(area1));
				}
				result.add(map);
			}
		}
		return result;
	}
	
	public List<Area> getAreaList(int parentId){
		if(parentId == 0){
			return areaRepository.findByParentIsNullOrderByIdAsc();
		} else {
			Area area = areaRepository.getOne(parentId);
			return areaRepository.findByParentOrderByIdAsc(area);
		}
	}
}
